using IcsSEMCtrl.Global.Device;
using IcsSEMCtrl.Util.Client;
using Lifter.Device;
using Lifter.Device.Dispatcher;
using Lifter.Event.Lifter;
using Lifter.Global;
using Lifter.Gui.Monitor.Panels;
using Lifter.Gui.Monitor.Popups;
using Lifter.Gui.Widget;
using Lifter.Utility;
using Sunny.UI;
using Sunny.UI.Demo;
using System;
using System.Windows.Forms;
using TKT.Lifter.CIM.Lifter.Global;
using TKT.Lifter.CIM.Lifter.Gui.Monitor.Panels;
using TKT.Lifter.CIM.Lifter.Gui.Monitor.Popups;

namespace Lifter.Gui.Monitor
{
    [Serializable]
    public static class MonitorEventHandler
    {
        public static void OnMainTopPanel_Click(object sender, MainTopPanelEventArgs e)
        {
            LifterSEM lifterSEM = (LifterSEM)Naming.GetDeviceManager().GetDevice();
            if (lifterSEM.UserPrivilige)
            {
                lifterSEM.LoginTimeCount = 1;
            }
            /*            if (e.MenuIndex == MainTopPanelEventArgs.TOP_MENU_INDEX.Login)
                        {
                            DlgLogIn dlgLogIn = (DlgLogIn)SimpleCache.GetObject("Popup", "DlgLogIn");
                            if (dlgLogIn == null)
                            {
                                dlgLogIn = new DlgLogIn();
                                SimpleCache.AddObject("Popup", "DlgLogIn", dlgLogIn);
                                dlgLogIn.Click += new LoginFormClickEvent(PopupEventsHandler.OnLogInClick);
                            }
                            dlgLogIn.DialogInit();
                            dlgLogIn.Show();
                        }*/
            if (e.MenuIndex == MainTopPanelEventArgs.TOP_MENU_INDEX.Login)
            {
                //DlgLogIn dlgLogIn = (DlgLogIn)SimpleCache.GetObject("Popup", "DlgLogIn");
                FLogin login = (FLogin)SimpleCache.GetObject("Popup", "FLogin");
                if (login == null)
                {
                    login = new FLogin();
                    SimpleCache.AddObject("Popup", "FLogin", login);
                }
                login.UserName = "";
                login.Password = "";
                login.Show();
            }
            if (e.MenuIndex == MainTopPanelEventArgs.TOP_MENU_INDEX.OnOffline)
            {
                DlgControlState dlgControlState = (DlgControlState)SimpleCache.GetObject("Popup", "DlgControlState");
                if (dlgControlState == null)
                {
                    dlgControlState = new DlgControlState();
                    SimpleCache.AddObject("Popup", "DlgControlState", dlgControlState);
                    //dlgControlState.Click += new DlgControlStateClickEvent(PopupEventsHandler.OnControlStateClick);
                }
                dlgControlState.DlgControlStateInit();
                dlgControlState.Show();
            }
        }
        public static void OnInformPanelClick(object sender, InformPanelEventArgs e)
        {
            LifterSEM lifterSEM = (LifterSEM)Naming.GetDeviceManager().GetDevice();
            if (lifterSEM.UserPrivilige)
            {
                lifterSEM.LoginTimeCount = 1;
            }
            InformCarrierDB informCarrierDB = (InformCarrierDB)SimpleCache.GetObject("InformPanel", "InformCarrierDB");
            InformTransfer informTransfer = (InformTransfer)SimpleCache.GetObject("InformPanel", "InformTransfer");
            InformOperation informOperation = (InformOperation)SimpleCache.GetObject("InformPanel", "InformOperation");
            InformError informError = (InformError)SimpleCache.GetObject("InformPanel", "InformError");
            InformHost informHost = (InformHost)SimpleCache.GetObject("InformPanel", "InformHost");
            InformPLC informPLC = (InformPLC)SimpleCache.GetObject("InformPanel", "InformPLC");
            InformDebug informDebug = (InformDebug)SimpleCache.GetObject("InformPanel", "InformDebug");
            if (informCarrierDB.Visible)
            {
                informCarrierDB.Visible = false;
            }
            if (informTransfer.Visible)
            {
                informTransfer.Visible = false;
            }
            if (informOperation.Visible)
            {
                informOperation.Visible = false;
            }
            if (informError.Visible)
            {
                informError.Visible = false;
            }
            if (informHost.Visible)
            {
                informHost.Visible = false;
            }
            if (informPLC.Visible)
            {
                informPLC.Visible = false;
            }
            if (informDebug.Visible)
            {
                informDebug.Visible = false;
            }
            if (e.EventPanelType == InformPanelEventArgs.PanelEvent_Type.CarrierDB && !informCarrierDB.Visible)
            {
                informCarrierDB.lstViewCarrierLoad("");
                informCarrierDB.Visible = true;
            }
            if (e.EventPanelType == InformPanelEventArgs.PanelEvent_Type.TransferDB && !informTransfer.Visible)
            {
                informTransfer.lstViewTransferQueue();
                informTransfer.Visible = true;
            }
            if (e.EventPanelType == InformPanelEventArgs.PanelEvent_Type.PLC && !informPLC.Visible)
            {
                informPLC.Visible = true;
            }
            if (e.EventPanelType == InformPanelEventArgs.PanelEvent_Type.Error && !informError.Visible)
            {
                informError.Visible = true;
            }
            if (e.EventPanelType == InformPanelEventArgs.PanelEvent_Type.Host && !informHost.Visible)
            {
                informHost.Visible = true;
            }
            if (e.EventPanelType == InformPanelEventArgs.PanelEvent_Type.Operation && !informOperation.Visible)
            {
                informOperation.Visible = true;
            }
            if (e.EventPanelType == InformPanelEventArgs.PanelEvent_Type.Debug && !informDebug.Visible)
            {
                informDebug.Visible = true;
            }
        }
        public static void OnMainBottomPanel(object sender, BottomNavigationEventArgs e)
        {
            LifterSEM lifterSEM = (LifterSEM)Naming.GetDeviceManager().GetDevice();
            if (lifterSEM.UserPrivilige)
            {
                lifterSEM.LoginTimeCount = 1;
            }
            LifterMainForm lifterMainForm = (LifterMainForm)SimpleCache.GetObject("FormMain", "LifterMainForm");
            InformPanel informPanel = (InformPanel)SimpleCache.GetObject("FormMain", "InformPanel");
            AllConfigControl allConfigControl = (AllConfigControl)SimpleCache.GetObject("FormMain", "AllConfigControl");
            AllHistoryControl allHistoryControl = (AllHistoryControl)SimpleCache.GetObject("FormMain", "AllHistoryControl");
            SystemsDisplay systemsDisplay = (SystemsDisplay)SimpleCache.GetObject("FormMain", "SystemsDisplay");
            OPPanel oPPanel = (OPPanel)SimpleCache.GetObject("FormMain", "OperationPanel");
            if (e.MenuIndex == BottomNavigationEventArgs.BOTTOM_MENU_INDEX.SHUTDOWN)
            {
                if (lifterSEM.UserPrivilige)
                {
                    lifterMainForm.ShutdownSystem();
                }
                else
                {
                    MonitorEventHandler.OnPopupHide();
                    PopupNotice.DialogPopups("请先登录", "Please login", enumPopupsType.Warning);
                }
            }
            else
            {
                if (e.MenuIndex != BottomNavigationEventArgs.BOTTOM_MENU_INDEX.INFORM && informPanel.Visible)
                {
                    informPanel.Visible = false;
                }
                if (e.MenuIndex != BottomNavigationEventArgs.BOTTOM_MENU_INDEX.ALLCONFIG && allConfigControl.Visible)
                {
                    allConfigControl.Visible = false;
                }
                if (e.MenuIndex != BottomNavigationEventArgs.BOTTOM_MENU_INDEX.ALLHISTORY && allHistoryControl.Visible)
                {
                    allHistoryControl.Visible = false;
                }
                if (e.MenuIndex != BottomNavigationEventArgs.BOTTOM_MENU_INDEX.SYSTEMDISPLAY && systemsDisplay.Visible)
                {
                    systemsDisplay.Visible = false;
                }
                if (e.MenuIndex != BottomNavigationEventArgs.BOTTOM_MENU_INDEX.OPERATION && oPPanel.Visible)
                {
                    oPPanel.Visible = false;
                }
                if (e.MenuIndex == BottomNavigationEventArgs.BOTTOM_MENU_INDEX.INFORM && !informPanel.Visible)
                {
                    informPanel.Visible = true;
                }
                if (e.MenuIndex == BottomNavigationEventArgs.BOTTOM_MENU_INDEX.ALLCONFIG && !allConfigControl.Visible)
                {
                    allConfigControl.Visible = true;
                }
                if (e.MenuIndex == BottomNavigationEventArgs.BOTTOM_MENU_INDEX.ALLHISTORY && !allHistoryControl.Visible)
                {
                    allHistoryControl.Visible = true;
                }
                if (e.MenuIndex == BottomNavigationEventArgs.BOTTOM_MENU_INDEX.SYSTEMDISPLAY && !systemsDisplay.Visible)
                {
                    systemsDisplay.Visible = true;
                }
                if (e.MenuIndex == BottomNavigationEventArgs.BOTTOM_MENU_INDEX.OPERATION && !oPPanel.Visible)
                {
                    oPPanel.Visible = true;
                }
            }
        }
        /// <summary>
        /// 主界面搬送命令控件改变事件
        /// 1.修改搬送命令优先级，只有优先级大于50的才能降低，优先级小于99的才能增高
        /// 2.删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void OnTransferQueueChange(object sender, MainTransferQueuePanelEventArgs e)
        {
            LifterSEM lifterSEM = (LifterSEM)Naming.GetDeviceManager().GetDevice();
            if (lifterSEM.UserPrivilige)
            {
                lifterSEM.LoginTimeCount = 1;
            }
            TransferWorkItem job = lifterSEM.LDispatcher.GetJob(e.CommandID);
            lock (lifterSEM.LDispatcher.workList_monitor)
            {
                if (job != null)
                {
                    //降低优先级
                    if (e.MenuIndex == MainTransferQueuePanelEventArgs.TRANSFER_Q_INDEX.PRIORITY_DOWN)
                    {
                        //判断搬送命令状态是否为CREATE /READY /NOTREADY
                        if (lifterSEM.LDispatcher.IsReadyForTransferState(job.TransferState))
                        {
                            //优先级大于50才能改变，否则不变
                            if (job.Priority > 50)
                            {
                                job.Priority--;
                                Naming.PublishEvent(TransferEvent.FormSubject("*"), new TransferEvent(lifterSEM.ElementId, EquipmentType.ValueToString(lifterSEM.EquipmentType), job.CommandID, TransferEvent.TransferEventType.UPDATE_TRANSFER));
                            }
                        }
                    }
                    //增加优先级
                    if (e.MenuIndex == MainTransferQueuePanelEventArgs.TRANSFER_Q_INDEX.PRIORITY_UP)
                    {
                        if (lifterSEM.LDispatcher.IsReadyForTransferState(job.TransferState))
                        {
                            if (job.Priority < 99)
                            {
                                job.Priority++;
                                Naming.PublishEvent(TransferEvent.FormSubject("*"), new TransferEvent(lifterSEM.ElementId, EquipmentType.ValueToString(lifterSEM.EquipmentType), job.CommandID, TransferEvent.TransferEventType.UPDATE_TRANSFER));
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 根据事件触发，隐藏PortInfomationDetail页面
        /// </summary>
        public static void OnPopupHide()
        {
            object[] objectAll = SimpleCache.GetObjectAll("Popup");
            object[] array = objectAll;
            for (int i = 0; i < array.Length; i++)
            {
                object obj = array[i];
                Form form = (Form)obj;
                if (form.Text == "PortInfomationDetail")
                {
                    PortInfomationDetail portInfomationDetail = (PortInfomationDetail)SimpleCache.GetObject("Popup", "PortInfomationDetail");
                    portInfomationDetail.OnEventTerminate();
                }
                form.Hide();
            }
        }
    }
}
